package fr.cephb.joperon.webapp.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import fr.cephb.svg.SVG;
import fr.cephb.util.XMLUtilities;
import fr.cephb.xlink.XLink;

public class SVGSimpleTrack  extends TagSupport
	{
	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_WIDTH=150;
	private static final int DEFAULT_HEIGHT=30;
	/** svg Width */
	private int width=DEFAULT_WIDTH;
	/** svg Height */
	private int height=DEFAULT_HEIGHT;
	/** minX */
	private int chromMin=0;
	/** maxX */
	private int chromMax=0;
	/** minX */
	private int chromStart=-1;
	/** maxX */
	private int chromEnd=-1;
	/** URL */
	private String url=null;
	/** URL */
	private String title=null;
	/***
	 * SVGSimpleTrack
	 */
	public SVGSimpleTrack()
		{
		
		}
	
	@Override
	public int doStartTag() throws JspException
		{
		return SKIP_BODY;
		}
	
	@Override
	public int doEndTag() throws JspException
		{
		final double ratio=0.8;
		if(		this.chromMin>=this.chromMax ||
				this.chromStart> this.chromMax ||
				this.chromEnd< this.chromMin)
			{
			return EVAL_PAGE;
			}
		
		try {
			java.io.Writer out=super.pageContext.getOut();
			out.write("<s:svg xmlns:s=\""+SVG.NS+"\" xmlns:xlink=\""+XLink.NS+"\"  width=\""+this.width+"\" height=\""+this.height+"\" " +
				" version=\"1.1\""+
				" style=\"stroke:gray;stroke-width:1;\""+
				">"+
				"<s:defs><s:linearGradient id='grad' x1='50%' y1='0%' x2='50%' y2='100%'>"+
				"<s:stop offset='0%' style='stop-color:black;stop-opacity:1'/>"+
				"<s:stop offset='50%' style='stop-color:white;stop-opacity:1'/>"+
				"<s:stop offset='100%' style='stop-color:black;stop-opacity:1'/>"+
			    "</s:linearGradient>"+
			  "</s:defs><s:g>"
			 );
			double w = this.width*ratio;
			double h= this.height*ratio;
			double marginV= (this.width-w)/2.0;
			double marginH= (this.height-h)/2.0;
			double x1= marginV+((Math.max(chromStart,chromMin)-chromMin)/(double)(chromMax-chromMin))*w;
			double x2= marginV+((Math.min(chromEnd,chromMax)-chromMin)/(double)(chromMax-chromMin))*w;
			
			out.write("<s:rect x='0' y='0' " +
					"width='"+ (this.width-1)+"' "+
					"height='"+ (this.height-1)+"' "+
					"style=\"fill:none;stroke:lightgray;\"/>");
			
			if(url!=null)
				{
				out.write("<s:a xlink:href='"+ XMLUtilities.escape(url)+"'>");
				}
			
			out.write("<s:rect x='"+(float)x1+"' y='"+(float)marginH+"' " +
				"height='"+ (float)h+"' "+
				"width='"+ (float)(x2-x1)+"' "+
				(this.title!=null?" title='"+XMLUtilities.escape(this.title)+"' ":"")+
				"style=\"fill:url(#grad);\"/>");
			if(url!=null)
				{
				out.write("</s:a>");
				}
			out.write("</s:g></s:svg>");
			} 
		catch (IOException err)
			{
			throw new JspException(err);
			}
		return EVAL_PAGE;
		}

	@Override
	public void release() {
		this.width=DEFAULT_WIDTH;
		this.height=DEFAULT_HEIGHT;
		this.chromEnd=-1;
		this.chromMax=-1;
		this.chromMin=-1;
		this.chromStart=-1;
		this.url=null;
		this.title=null;
		}
	
	
	
	public void setMin(int chromMin) {
		this.chromMin = chromMin;
	}

	public void setMax(int chromMax) {
		this.chromMax = chromMax;
	}

	public void setStart(int chromStart) {
		this.chromStart = chromStart;
	}

	public void setEnd(int chromEnd) {
		this.chromEnd = chromEnd;
	}

	public void setWidth(int width) {
		this.width = width;
	}
	
	public void setHeight(int height) {
		this.height = height;
	}
	
	public void setUrl(String url) {
		this.url = url;
		}
	
	public void setTitle(String title) {
		this.title = title;
		}
	}
